package vip.xianz.jobmanager.service.impl;

import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xianz.jobmanager.data.entity.UserLoginLog;
import vip.xianz.jobmanager.data.entity.XUser;
import vip.xianz.jobmanager.data.vo.ResultBean;
import vip.xianz.jobmanager.mapper.UserLoginLogMapper;
import vip.xianz.jobmanager.mapper.XUserMapper;
import vip.xianz.jobmanager.service.IXUserService;
import vip.xianz.jobmanager.util.PrimaryKeyUtils;

import java.time.Instant;

/**
 * 用户管理接口实现
 *
 * @author xian5
 * @create 2019-05-06  21:16
 */
@Service
public class XUserServiceImpl implements IXUserService {
    @Autowired
    private XUserMapper userMapper;
    @Autowired
    private UserLoginLogMapper logMapper;

    @Override
    public XUser getUserById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Transactional
    @Override
    public Boolean addUser(XUser user) {
        return userMapper.insertSelective(user) != 0;
    }

    @Override
    public XUser getUserWithBaseInfo(String userId) {
        return userMapper.selectBaseInfoByPrimaryKey(userId);
    }

    @Transactional
    @Override
    public Boolean updateUser(XUser user) {
        return userMapper.updateByPrimaryKeySelective(user) == 0 ? Boolean.FALSE : Boolean.TRUE;
    }

    @Transactional
    @Override
    public Boolean updateLoginAndRecoderLog(String userId, UserLoginLog log) {
        int firstInsert = userMapper.updateLastLoginTimeAndIpById(userId, log.getLoginTime(), log.getUserIp());
        // 记录用户登录日志
        int lastInsert = logMapper.insert(log);
        return firstInsert != 0 && firstInsert == lastInsert;
    }
}
